﻿using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;


namespace GTAuto.DAL.DAL
{
    public class DALAdPictures : DALBase
    {
        public static DALAdPictures Singleton = new DALAdPictures();

        public GTAdsPicture Add(GTAdsPicture gtAdsPicture)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newPic = new AdsPicture();

                newPic.AdsId = gtAdsPicture.Id;
                newPic.Path = gtAdsPicture.Path;
                newPic.Status = gtAdsPicture.Status;
                newPic.Type = gtAdsPicture.Type;

                db.AdsPictures.InsertOnSubmit(newPic);
                db.SubmitChanges();
                gtAdsPicture.Id = newPic.Id;
                return gtAdsPicture;
            }
        }

        public GTAdsPicture Update(GTAdsPicture gtAdsPicture)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newPic = db.AdsPictures.SingleOrDefault(x => x.Id == gtAdsPicture.Id);

                newPic.AdsId = gtAdsPicture.Id;
                newPic.Path = gtAdsPicture.Path;
                newPic.Status = gtAdsPicture.Status;
                newPic.Type = gtAdsPicture.Type;
                db.SubmitChanges();

                return gtAdsPicture;
            }
        }


        public GTAdsPicture Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.AdsPictures.SingleOrDefault(x => x.Id == id);
                GTAdsPicture gtBrand = new GTAdsPicture();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTAdsPicture> GetByAdsId(int adId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.AdsPictures
                 where p.AdsId == adId
                 select p;
                
                 var brands = brds.ToList();
                
                 var ret = new List<GTAdsPicture>();
                foreach (var brand in brands)
                {
                    GTAdsPicture gtBrand = new GTAdsPicture();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }
        


        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.AdsPictures.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.AdsPictures.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
